package com.kernal.smartvision.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
//import android.support.annotation.NonNull;
//import android.support.v4.app.ActivityCompat;
//import android.support.v4.content.ContextCompat;
//import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.kernal.smartvision.R;
import com.kernal.smartvision.fragment.BasicFragment;
import com.kernal.smartvision.ocr.Devcode;
import com.kernal.smartvision.utils.PermissionUtils;
import com.kernal.smartvisionocr.RecogService;
import com.kernal.smartvisionocr.utils.Utills;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 导入识别
 * Created by user on 2018/3/28.
 */
public class PictureRecogActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback, BasicFragment.FragmentSelectPicture {
    public RecogService.MyBinder recogBinder;
    private int iTH_InitSmartVisionSDK = -1;
    private String Imagepath;
    private Bitmap bitmap;
    private int width, herght;
    private int[] nCharCount = new int[2];// 返回结果中的字符数
    public ProgressDialog mypDialog;
    private String uploadPicPath;
    private final int SELECT_RESULT_CODE = 3;
    private static final int REQUEST_PICK_IMAGE = 4;


    private BasicFragment fragment;
    private boolean useCrop = false;
    private boolean isRecognazing = false;

    public ServiceConnection recogConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            recogConn = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            recogBinder = (RecogService.MyBinder) service;
            iTH_InitSmartVisionSDK = recogBinder.getInitSmartVisionOcrSDK();
            if (iTH_InitSmartVisionSDK == 0) {
                recogBinder.AddTemplateFile();// 添加识别模板


                /**
                 * 模板分为三种：
                 * vin 码扫描识别模板id    : SV_ID_VIN_CARWINDOW(已经在 OcrVin.java 中配置好)
                 * 手机号码扫描识别模板id  : SV_IPD_YYZZ_MOBILEHONE(已经在 OcrPhoneNumber 中配置好)
                 * vin 码导入识别模板id    :SV_ID_VIN_MOBILE(导入识别只支持vin码识别，在这里配置)
                 */
                recogBinder.SetCurrentTemplate("SV_ID_VIN_MOBILE");// 设置当前识别模板ID为 vin 码扫描识别的 id

                if (bitmap != null) {
                    //裁剪会直接走这里

                    uploadPicPath = getSavePath(bitmap);
                    Imagepath = uploadPicPath;
                    LoadBitmapForFile();

                } else {
                    if (Imagepath != null) {
                        ContentResolver cr = getContentResolver();
                        ParcelFileDescriptor parcelFileDescriptor = null;
                        try {
                            parcelFileDescriptor = cr.openFileDescriptor(com.kernal.smartvision.utils.Utills.getImageContentUri(getApplicationContext(), Imagepath), "r");
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            try {
                                parcelFileDescriptor = cr.openFileDescriptor(Uri.parse("file://" + Imagepath), "r");
                            } catch (FileNotFoundException ex) {
                                ex.printStackTrace();
                            }

                        }
                        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                        bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                        if (bitmap != null) {
                            LoadBitmapForFile();
                        }

                    }
                }


                /***
                 * @param bAppWantDeepLearning boolean ,是否开启深度学习，默认为开启，如果想要关闭设置为false即可
                 * recogBinder.Recognize(Devcode.devcode, ocrTypeHelper.ocrId,bAppWantDeepLearning);
                 */

                int returnRecogize = recogBinder.Recognize(Devcode.devcode, "SV_ID_VIN_MOBILE");
                String recogResultString = recogBinder.GetResults(nCharCount);
                if (recogResultString == null || "".equals(recogResultString)) {
                    if (returnRecogize != 0) {
                        recogResultString = "识别失败，错误代码为:" + returnRecogize;
                    } else {
                        recogResultString = "识别失败";
                    }
                } else {
                    //识别成功的
                    Imagepath = savePicture(true);
                }
                Intent intent = new Intent();
                intent.putExtra("RecogResult", recogResultString);
                //vin
                intent.putExtra("ocrType", 0);
                intent.putExtra("resultPic", Imagepath);
                intent.putExtra("uploadPicPath", uploadPicPath);
                setResult(Activity.RESULT_OK, intent);
                finish();
            } else {
                Toast.makeText(PictureRecogActivity.this, "核心初始化失败，错误码：" + iTH_InitSmartVisionSDK, Toast.LENGTH_LONG).show();
                finish();
            }
        }

        private void LoadBitmapForFile() {
            int bytes = bitmap.getByteCount();
            ByteBuffer buffer = ByteBuffer.allocate(bytes);
            bitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
            byte[] pImageData = buffer.array();
            width = bitmap.getWidth();
            herght = bitmap.getHeight();
            recogBinder.LoadImageFile(pImageData, width, herght, bitmap.getRowBytes());
        }
    };

    public static final String[] PERMISSION = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,// 写入权限
            Manifest.permission.READ_EXTERNAL_STORAGE, // 读取权限
            Manifest.permission.CAMERA,
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_pic_recog);

        //判断是否需要动态授权
        if (Build.VERSION.SDK_INT >= 23) {
            //先进行权限申请
            permission();
        } else {
            //不需要动态授权
            setlectPicAndRecog();
        }
    }

    /**
     * 选择图片进行识别
     */
    private void setlectPicAndRecog() {
        // 将资源文件写入本地

        Utills.copyFile(this);
        Intent selectIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        Intent wrapperIntent = Intent.createChooser(selectIntent, "请选择一张图片");
        try {
            startActivityForResult(wrapperIntent, SELECT_RESULT_CODE);
        } catch (Exception e) {
            Toast.makeText(PictureRecogActivity.this, "请安装文件管理器", Toast.LENGTH_SHORT).show();
        }
    }

    @SuppressLint("NewApi")
    public void CreateDialog(Context context) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.7f;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mypDialog = new ProgressDialog(context);
        mypDialog.setProgressStyle(context.getResources().getIdentifier("dialog", "styles", context.getPackageName()));
        mypDialog.setMessage("识别中.....");
        mypDialog.show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (recogBinder != null) {
            unbindService(recogConn);
            recogBinder = null;
        }

        if (mypDialog != null) {
            mypDialog.dismiss();
        }
    }

    /**
     * 保存图片
     *
     * @return
     */
    public String savePicture(boolean type) {
        String picPathString1 = "";
        String PATH = getFilesDir().getPath() + "/DCIM/Camera/";
        File file = new File(PATH);
        if (!file.exists()) {
            file.mkdirs();
        }
        String name = Utills.pictureName();
        if (type) {
            picPathString1 = PATH + "smartVisition" + name + ".jpg";
            // 识别区域内图片
            recogBinder.svSaveImageResLine(picPathString1);
        } else {
            picPathString1 = PATH + "scv" + name + ".jpg";
            recogBinder.svSaveImage(picPathString1);
        }
        File temp = new File(picPathString1);
        if (temp.length() <= 0) {
            picPathString1 = uploadPicPath;
        }
        return picPathString1;
    }

    @NonNull
    private String getSavePath(Bitmap cropped) {
        long currentTimeMillis = System.currentTimeMillis();
        Date today = new Date(currentTimeMillis);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String title = dateFormat.format(today);
        String dirPath = getFilesDir().getPath() + "/DCIM/Camera/";
        String fileName = "scv" + title + ".jpg";
        String path = dirPath + "/" + fileName;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (file.exists()) {
            file.delete();
        }
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            cropped.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }

        return path;
    }

    /**
     * 权限申请
     */
    private void permission() {
        boolean isgranted = true;
        for (int i = 0; i < PERMISSION.length; i++) {
            if (ContextCompat.checkSelfPermission(getApplicationContext(), PERMISSION[i]) != PackageManager.PERMISSION_GRANTED) {
                isgranted = false;
                break;
            }
        }
        if (!isgranted) {
            //没有授权
            PermissionUtils.requestMultiPermissions(this, mPermissionGrant);
        } else {
            setlectPicAndRecog();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == SELECT_RESULT_CODE || requestCode == REQUEST_PICK_IMAGE) && resultCode == Activity.RESULT_OK) {
            String picPathString = null;
            Uri uri = data.getData();
            picPathString = com.kernal.smartvision.utils.Utills.getPath(getApplicationContext(), uri);
            if (picPathString != null && !picPathString.equals("") && !picPathString.equals(" ") && !picPathString.equals("null")) {
                if (picPathString.endsWith(".jpg") || picPathString.endsWith(".JPG") || picPathString.endsWith(".png") || picPathString.endsWith(".PNG") || picPathString.endsWith(".jpeg")) {
                    String tempPath = picPathString;
                    /**
                     * 可以直接用从图库选择的图片进行识别，也可以对图片进行裁剪等操作后进行识别,如果不想裁切图片，把下面代码注释即可
                     */
                    Bundle bundle = new Bundle();
                    bundle.putString("DATA", uri.toString());
                    if (fragment == null) {
                        useCrop = true;
                        fragment = BasicFragment.newInstance();
                        fragment.setArguments(bundle);
                        getSupportFragmentManager().beginTransaction().add(R.id.pic_container, fragment).commit();
                    } else {
                        useCrop = true;
                        fragment = BasicFragment.newInstance();
                        fragment.setArguments(bundle);
                        getSupportFragmentManager().beginTransaction().replace(R.id.pic_container, fragment).commit();
                    }
                    /**
                     * 开始识别
                     */
                    if (!useCrop) {
                        recogPic(tempPath);
                    }

                } else {
                    Toast.makeText(this, "请选择一张正确的图片", Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            backEvent();
        }
    }


    /**
     * 开始识别
     *
     * @param selectPicPath 选中的图片的路径
     */
    private void recogPic(String selectPicPath) {
        Imagepath = selectPicPath;
        uploadPicPath = selectPicPath;
        CreateDialog(this);
        if (recogBinder == null) {
            Intent authIntent = new Intent(this, RecogService.class);
            bindService(authIntent, recogConn, Service.BIND_AUTO_CREATE);
        }
    }



    /**
     * 裁切图片回调函数
     *
     * @param bitmap 裁切好的图片
     */
    @Override
    public void startRecog(Bitmap bitmap) {
        if (isRecognazing) {
            return;
        }
        isRecognazing = true;
        this.bitmap = bitmap;
        CreateDialog(this);
        if (recogBinder == null) {
            Intent authIntent = new Intent(this, RecogService.class);
            bindService(authIntent, recogConn, Service.BIND_AUTO_CREATE);
        }

    }


    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case PermissionUtils.CODE_MULTI_PERMISSION:
                    setlectPicAndRecog();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * Callback received when a permissions request has been completed.
     */
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            backEvent();
            return true;
        }
        return true;
    }

    private void backEvent() {
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
       /* if (Build.VERSION.SDK_INT > 18) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(option);
        }*/

    }


}
